package cn.sixmillions.ps.controller;


import cn.sixmillions.ps.common.aspect.CheckSecret;
import cn.sixmillions.ps.common.base.R;
import cn.sixmillions.ps.entity.MdPreview;
import cn.sixmillions.ps.service.IMdPreviewService;
import cn.sixmillions.ps.view.Obsius;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * markdown预览(TtMdPreview)表控制层
 *
 * @author six
 * @since 2023-01-12
 */
@RestController
@RequestMapping("/api/md-preview")
@RequiredArgsConstructor
@Slf4j
@CrossOrigin("*")
public class MdPreviewController {

    /**
     * 服务对象
     */
    final private IMdPreviewService mdPreviewService;

    @PostMapping
    public R<Obsius> save(@RequestBody MdPreview mdPreview) {
        MdPreview data = mdPreviewService.saveOneMd(mdPreview);
        return R.data(Obsius.builder().id(data.getMdId()).secret(data.getMdSecret()).build());
    }

    @GetMapping
    public R<MdPreview> detail(String mdId) {
        MdPreview detail = mdPreviewService.getById(mdId);
        if (detail == null) {
            return R.fail();
        }
        return R.data(detail);
    }

    @PutMapping
    @CheckSecret
    public R update(@RequestBody MdPreview mdPreview) {
        mdPreviewService.updateById(mdPreview);
        return R.success();
    }

    @DeleteMapping
    @CheckSecret
    public R remove(@RequestBody MdPreview mdPreview) {
        mdPreviewService.removeById(mdPreview.getMdId());
        return R.success();
    }

}

